﻿/**
 * 得到URL请求参数
 * @param variable URL键
 * @returns URL值
 */
function getQuery(variable)
{
       var query = window.location.search.substring(1);
       var vars = query.split("&");
       for (var i=0;i<vars.length;i++) {
               var pair = vars[i].split("=");
               if(pair[0] == variable){return pair[1];}
       }
       return(false);
}

/**
 * 根据ID得到DOM对象
 * @param id 元素ID
 * @returns DOM对象
 */
function $(id){
    return document.getElementById(id);
}

/**
 * 图片预览
 * @param fileId 文件表单ID
 * @param imgId 图片ID
 */
function fileChange(fileId,imgId){
    var reader = new FileReader();//定义文件读取流对象
    reader.readAsDataURL(document.getElementById(fileId).files[0]);
    reader.onload = function (ev) {
        document.getElementById(imgId).src = ev.target.result;
    }
}

/**
 * 得到表单提交数据对象
 * @param formId 表单ID
 */
function getFormData(formId){
    var childArray = $(formId).childNodes;
    var param = {};
    for(var i=0;i<childArray.length;i++){
        var obj = childArray[i];
        if(obj.name != ''){
            var fieldName = obj.name;
            if(obj.type == "radio" ){
                if(obj.checked == true){
                    param[fieldName] = obj.value;
                }
                continue;
            }

            if(param[fieldName] == undefined){
                param[fieldName] = obj.value;
            }
            else{
                if(param[fieldName] instanceof Array){
                    param[fieldName].push(obj.value);
                }
                else{
                    var value = param[fieldName];
                    param[fieldName] = [];
                    param[fieldName].push(value);
                    param[fieldName].push(obj.value);
                }
            }

        }
    }
    return param;
}